package cn.itcast.statistics.service.impl;

import cn.itcast.commonutils.R;
import cn.itcast.statistics.client.UCenterClient;
import cn.itcast.statistics.entity.Daily;
import cn.itcast.statistics.mapper.DailyMapper;
import cn.itcast.statistics.service.DailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author Duanzihuang
 * @since 2020-06-23
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {

    @Autowired
    private UCenterClient uCenterClient;

    @Override
    public void countRegister(String day) {
        R registerR = uCenterClient.getRegisterCount(day);

        Integer registerCount = (Integer)registerR.getData().get("registerCount");

        if (registerCount > 0) {
            // 先删除统计表中同一日期的数据
            QueryWrapper<Daily> wrapper = new QueryWrapper<>();
            wrapper.eq("date_calculated",day);
            baseMapper.delete(wrapper);

            // 添加某一个日期的数据
            Daily daily = new Daily();
            daily.setDateCalculated(day);
            daily.setRegisterNum(registerCount);
            daily.setLoginNum(RandomUtils.nextInt(100,200));
            daily.setVideoViewNum(RandomUtils.nextInt(100,200));
            daily.setCourseNum(RandomUtils.nextInt(100,200));
            baseMapper.insert(daily);
        }
    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        // 根据条件查出满足条件的列表
        QueryWrapper<Daily> wrapper = new QueryWrapper();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);
        List<Daily> dailies = baseMapper.selectList(wrapper);

        // 生成前端页面所需要的map
        List<String> dates = new ArrayList<>();
        List<Integer> nums = new ArrayList<>();
        for (int i = 0; i < dailies.size(); i++) {
            Daily daily = dailies.get(i);

            dates.add(daily.getDateCalculated());

            switch (type) {
                case "register_num":
                    nums.add(daily.getRegisterNum());
                    break;

                case "login_num":
                    nums.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    nums.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    nums.add(daily.getCourseNum());
                    break;
            }
        }

        // 最终返回的数据
        Map<String,Object> map = new HashMap<>();
        map.put("dates",dates);
        map.put("nums",nums);

        return map;
    }
}
